package testcase.command;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.NoSuchElementException;

import org.junit.Test;

import satir.card.Choice;
import satir.card.Question;
import satir.command.AskQuestionCmd;
import satir.player.Player;
import junit.framework.TestCase;


public class In_AskQuestionCmd_Choice_1 extends TestCase{
	
	/*
	 * Test step:1
	 * Objective: Test integration of AskQuestionCmd and Choice, to print out choices of event card one 
	 */
	@Test
		public void testQuestion1Choice(){ 
		class QuestionStub extends Question{
			
			public QuestionStub() {
				super (null, null);
			}
			private ArrayList<Choice> choices;
			@Override
			public String getQuestion(){
				return "Mother notices her children talk with classmate at night about 30 mins or more. As Father, what is your decision?";
			}
			
			@Override
			public ArrayList<Choice> getChoices() {
				choices = new ArrayList<Choice>();
				choices.add(new Choice("Blame him/her and prohibit use the phone more than 5 mins.", 1,new Hashtable<String, Integer>()));
				choices.add(new Choice("Talk with son/daughter and find the reason.", 1, new Hashtable<String, Integer>()));
				choices.add(new Choice("Threaten son/daughter. If the situation still not change, you will modify the amount of pocket money.", -1, new Hashtable<String, Integer>()));
				choices.add(new Choice("Ignore him/her.", -1, new Hashtable<String, Integer>()));
				return choices;
			}
		};
		
		class PlayerStub extends Player{
			private int selfEsteem;
			public PlayerStub(){
				selfEsteem=5;
			}
			@Override
			public void increaseSelfEsteem(int increaseValue){
				selfEsteem+=increaseValue;
			}
		};
		
		PlayerStub playerStub = new PlayerStub();
		QuestionStub questionStub = new QuestionStub();
		AskQuestionCmd askQuestionCmd = new AskQuestionCmd(questionStub, playerStub);
		
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		PrintStream original = System.out;
		System.setOut(new PrintStream(systemOut)); // Start store the output here
		String input = "1";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		try {
			askQuestionCmd.execute();
		} catch (NoSuchElementException e) {
		}
		System.setOut(original); // Store the output unit here
		
		String actualResult = systemOut.toString();
		int startIndex = 0;
		String actualQuestion = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice1 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice2 =actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice3 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice4 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		
		boolean result = actualChoice1.equals("1. Blame him/her and prohibit use the phone more than 5 mins.")
						&& actualChoice2.equals("2. Talk with son/daughter and find the reason.")
						&& actualChoice3.equals("3. Threaten son/daughter. If the situation still not change, you will modify the amount of pocket money.")
						&& actualChoice4.equals("4. Ignore him/her.");
		assertEquals(true,result);
}

	/*
	 * Test step:2
	 * Objective: Test integration of AskQuestionCmd and Choice, to print out choices of event card two
	 */
	@Test
		public void testQuestion2Choice(){ 
		class QuestionStub extends Question{
			
			public QuestionStub() {
				super (null, null);
			}
			private ArrayList<Choice> choices;
			@Override
			public String getQuestion(){
				return "Your children is facing the examination, Father prohibits his/her go outside since his/her Chinese subject result is only got 60 marks\nBut he/she is invited to attend the brithday party on this Saturday. As childern, What is your decision?";
			}
			
			@Override
			public ArrayList<Choice> getChoices() {
				choices = new ArrayList<Choice>();
				choices.add(new Choice("You decide to go and lie to parent.", -2, new Hashtable<String, Integer>()));
				choices.add(new Choice("Talk with son/daughter and find the reason.", 1, new Hashtable<String, Integer>()));
				choices.add(new Choice("You will discuss with parent about the revision time.", 5, new Hashtable<String, Integer>()));
				choices.add(new Choice("Ask bother or sister to give advice.", 1, new Hashtable<String, Integer>()));
				return choices;
			}
		};
		
		class PlayerStub extends Player{
			private int selfEsteem;
			public PlayerStub(){
				selfEsteem=5;
			}
			@Override
			public void increaseSelfEsteem(int increaseValue){
				selfEsteem+=increaseValue;
			}
		};
		
		PlayerStub playerStub = new PlayerStub();
		QuestionStub questionStub = new QuestionStub();
		AskQuestionCmd askQuestionCmd = new AskQuestionCmd(questionStub, playerStub);
		
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		PrintStream original = System.out;
		System.setOut(new PrintStream(systemOut)); // Start store the output here
		String input = "1";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		try {
			askQuestionCmd.execute();
		} catch (NoSuchElementException e) {
		}
		System.setOut(original); // Store the output unit here
		
		String actualResult = systemOut.toString();
		int startIndex = 0;
		int Index = actualResult.indexOf('\n', startIndex);
		String actualQuestion = actualResult.substring(startIndex, actualResult.indexOf('\n', Index+1)-1);
		startIndex = actualResult.indexOf('\n', Index+1)+1;
		String actualChoice1 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice2 =actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice3 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		startIndex = actualResult.indexOf('\n', startIndex)+1;
		String actualChoice4 = actualResult.substring(startIndex,actualResult.indexOf('\n', startIndex)-1);
		
		boolean result = actualChoice1.equals("1. You decide to go and lie to parent.")
						&& actualChoice2.equals("2. Talk with son/daughter and find the reason.")
						&& actualChoice3.equals("3. You will discuss with parent about the revision time.")
						&& actualChoice4.equals("4. Ask bother or sister to give advice.");
		assertEquals(true,result);
}

}